{extend name="_container/add"}

{block name="content"}

<div class="layui-form-item">
    <label class="layui-form-label required">主表<span></span></label>
    <div class="layui-input-inline">
        <select name="table_id_master" class="select2" lay-ignore id="table_id_master" style="width: 100%" lay-verify="required">
            <option value="">请选择</option>
            {foreach $tables as $table}
            <option value="{$table.id}">{$table.comment}</option>
            {/foreach}
        </select>
        <div class="description"></div>
    </div>
    <div class="layui-form-mid layui-word-aux"></div>
</div>
<div class="layui-form-item">
    <label class="layui-form-label required">类型</label>
    <div class="layui-input-inline">
        <select name="type" class="select2" lay-ignore id="type" style="width: 100%" lay-verify="required">
            <option value="">请选择</option>
            {foreach $types as $value=>$name}
            <option value="{$value}">{$name}</option>
            {/foreach}
        </select>
    </div>
    <div class="layui-form-mid layui-word-aux"></div>
</div>

<div class="layui-form-item" id="middle_wrapper" hidden>
    <label class="layui-form-label required"><span style="color: #DD691D">（通过）</span>中间表<span></span></label>
    <div class="layui-input-inline">
        <select name="table_id_middle" class="select2" lay-ignore id="table_id_middle" style="width: 100%" >
            <option value="">请选择</option>
            {foreach $tables as $table}
            <option value="{$table.id}">{$table.comment}</option>
            {/foreach}
        </select>
        <div class="description"></div>
    </div>
    <div class="layui-form-mid layui-word-aux"></div>
</div>

<div class="layui-form-item">
    <label class="layui-form-label required"><span style="color: #DD691D" id="relation"></span>副表<span></span></label>
    <div class="layui-input-inline">
        <select name="table_id_sliver" class="select2" lay-ignore id="table_id_sliver" style="width: 100%" lay-verify="required">
            <option value="">请选择</option>
            {foreach $tables as $table}
            <option value="{$table.id}">{$table.comment}</option>
            {/foreach}
        </select>
        <div class="description"></div>
    </div>
    <div class="layui-form-mid layui-word-aux"></div>
</div>




{/block}
{block name="js"}
<script>
    function save_form3(obj) {
        save_form2(obj);
        return false;
    }
</script>



<script>
    let type_map =  {:json_encode($types)};
    $('#type').change(
        () => {
            let type = $("#type").val();
           if(type === 'hasManyThrough' || type === 'hasOneThrough') {
               $("#middle_wrapper").show();
               $('#relation').html('（' + type_map[type] + '）')
           }else{
               if(type){
                   $('#relation').html('（' + type_map[type] + '）')
               } else {
                   $('#relation').html('')
               }

               $("#middle_wrapper").hide();
           }

        }
    )
</script>
{/block}

{/extend}
